package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.domain.SystemMenu;
import cn.supermaster.management.common.domain.SystemRole;
import cn.supermaster.management.common.entity.Ztree;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.core.util.AuthorizationUtils;
import cn.supermaster.management.system.service.ISystemMenuService;
import cn.supermaster.management.web.service.system.ISMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SMenuServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/12 0:35
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SMenuServiceImpl implements ISMenuService {

    private String prefix = "system/menu";

    @Autowired
    private ISystemMenuService menuService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [跳转菜单页面]
     * <p></P>
     * @Title: skipMenuIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Return: java.lang.String
     */
    @Override
    public String skipMenuIndexHtml() {
        return prefix + "/menu";
    }

    /**
     * @Description: [跳转菜单新增界面]
     * <p></P>
     * @Title: skipAddMenuHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: modelMap
     * @Param: parentId
     * @Return: java.lang.String
     */
    @Override
    public String skipAddMenuHtml(ModelMap modelMap, Long parentId) {
        SystemMenu menu = null;
        if (0L != parentId) {
            menu = menuService.selectMenuById(parentId);
        } else {
            menu = new SystemMenu();
            menu.setMenuId(0L);
            menu.setMenuName("主目录");
        }
        modelMap.put("menu", menu);
        return prefix + "/add";
    }

    /**
     * @Description: [跳转修改菜单界面]
     * <p></P>
     * @Title: skipModifyMenuHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: modelMap
     * @Param: menuId
     * @Return: java.lang.String
     */
    @Override
    public String skipModifyMenuHtml(ModelMap modelMap, Long menuId) {
        modelMap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/edit";
    }

    /**
     * @Description: [跳转选择菜单图标界面]
     * <p></P>
     * @Title: skipMenuIconHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Return: java.lang.String
     */
    @Override
    public String skipMenuIconHtml() {
        return prefix + "/icon";
    }

    /**
     * @Description: [跳转选择菜单树界面]
     * <p></P>
     * @Title: skipSelectMenuTreeHtml
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: modelMap
     * @Param: menuId
     * @Return: java.lang.String
     */
    @Override
    public String skipSelectMenuTreeHtml(ModelMap modelMap, Long menuId) {
        modelMap.put("menu", menuService.selectMenuById(menuId));
        return prefix + "/tree";
    }

    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [根据条件获取菜单列表]
     * <p></P>
     * @Title: queryMenuListByCondition
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: menu
     * @Return: java.util.List<cn.supermaster.management.common.domain.SystemMenu>
     * @Throws:
     */
    @Override
    public List<SystemMenu> queryMenuListByCondition(SystemMenu menu) {
        Long userId = ShiroUtils.getUserId();
        List<SystemMenu> menuList = menuService.selectMenuList(menu, userId);
        return menuList;
    }

    /**
     * @Description: [删除菜单]
     * <p></P>
     * @Title: doRemoveMenuInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: menuId
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doRemoveMenuInformation(Long menuId) {
        if (menuService.selectCountMenuByParentId(menuId) > 0) {
            return WebResponse.warn("存在子菜单,不允许删除");
        }
        if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
            return WebResponse.warn("菜单已分配,不允许删除");
        }
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return WebHelper.webResult(menuService.deleteMenuById(menuId));
    }

    /**
     * @Description: [新增保存菜单]
     * <p></P>
     * @Title: doAddMenuInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: menu
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doAddMenuInformation(SystemMenu menu) {
        if (UserConstant.MENU_NAME_NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return WebHelper.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateBy(ShiroUtils.getuserName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return WebHelper.webResult(menuService.insertMenu(menu));
    }

    /**
     * @Description: [修改保存菜单]
     * <p></P>
     * @Title: doMofifyMenuInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: menu
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doMofifyMenuInformation(SystemMenu menu) {
        if (UserConstant.MENU_NAME_NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return WebHelper.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setUpdateBy(ShiroUtils.getuserName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return WebHelper.webResult(menuService.updateMenu(menu));
    }

    /**
     * @Description: [校验菜单名称]
     * <p></P>
     * @Title: doCheckMenuNameUnique
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: menu
     * @Return: java.lang.String
     */
    @Override
    public String doCheckMenuNameUnique(SystemMenu menu) {
        return menuService.checkMenuNameUnique(menu);
    }

    /**
     * @Description: [加载角色菜单列表树]
     * <p></P>
     * @Title: loadRoleMenuTreeInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Param: role
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     */
    @Override
    public List<Ztree> loadRoleMenuTreeInformation(SystemRole role) {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.roleMenuTreeData(role, userId);
        return ztrees;
    }

    /**
     * @Description: [加载所有菜单列表树]
     * <p></P>
     * @Title: loadAllMenuTreeInformation
     * @Author: Lance Ting
     * @Date: 2021-01-12
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     * @Throws:
     */
    @Override
    public List<Ztree> loadAllMenuTreeInformation() {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.menuTreeData(userId);
        return ztrees;
    }
}